﻿using System;
using System.Linq;

namespace gt.IdentityServer.Data
{
    public static class DataInitializer
    {
        public static void Initialize(AppIdentityContext context)
        {
            context.Database.EnsureCreated();
            if (context.Users.Any() && context.UserGroups.Any() && context.UserRoles.Any())
            {
                return;
            }

            var roles = new AppUserRole[] {
                new AppUserRole{ Name="系统管理员"},
                new AppUserRole{ Name="官员"}
            };
            context.UserRoles.AddRange(roles);
            context.SaveChanges();

            var sys_role_id = roles.First(x => x.Name == "系统管理员").Id;
            var guanyuan_role_id = roles.First(x => x.Name == "官员").Id;

            var groups = new AppUserGroup[]
            {
                new AppUserGroup{ Name="中央",RoleMappings=new System.Collections.Generic.List<AppGroupRoleMapping>{ new AppGroupRoleMapping {RoleId=sys_role_id } } },
                new AppUserGroup{ Name="魏国",RoleMappings=new System.Collections.Generic.List<AppGroupRoleMapping>{ new AppGroupRoleMapping {RoleId=guanyuan_role_id } }},
                new AppUserGroup{ Name="蜀国",RoleMappings=new System.Collections.Generic.List<AppGroupRoleMapping>{ new AppGroupRoleMapping {RoleId=guanyuan_role_id } }}
            };
            context.UserGroups.AddRange(groups);
            context.SaveChanges();

            var center_group_id = groups.First(x => x.Name == "中央").Id;
            var wey_group_id = groups.First(x => x.Name == "魏国").Id;
            var shu_group_id = groups.First(x => x.Name == "蜀国").Id;

            var users = new AppUser[]
            {
                new AppUser{ Age=29,RegsterTime=DateTime.Now,Introduce="测试账号",ShowName="gt1987",UserName="gt1987",Password="123456",Phone="18502225920",Gender=1,GroupId=center_group_id},

                new AppUser{ Age=41,RegsterTime=DateTime.Now,Introduce="蜀君主，仁义治天下",ShowName="刘备",UserName="liubei",Password="123456",Phone="13700000001",Gender=1,GroupId=shu_group_id,
                    RoleMappings=new System.Collections.Generic.List<AppUserRoleMapping>{ new AppUserRoleMapping {RoleId=sys_role_id }} },

                new AppUser{ Age=28,RegsterTime=DateTime.Now,Introduce="蜀丞相，鞠躬尽瘁死而后已",ShowName="诸葛亮",UserName="zhugeliang",Password="123456",Phone="13700000002",Gender=1,GroupId=shu_group_id},
                new AppUser{ Age=34,RegsterTime=DateTime.Now,Introduce="蜀五虎将，汉寿亭侯",ShowName="关羽",UserName="guanyu",Password="123456",Phone="13700000003",Gender=1,GroupId=shu_group_id},
                new AppUser{ Age=35,RegsterTime=DateTime.Now,Introduce="蜀五虎将，长坂之吼",ShowName="张飞",UserName="zhangfei",Password="123456",Phone="13700000004",Gender=1,GroupId=shu_group_id},
                new AppUser{ Age=23,RegsterTime=DateTime.Now,Introduce="蜀五虎将，七进七出",ShowName="赵云",UserName="zhaoyun",Password="123456",Phone="13700000005",Gender=1,GroupId=shu_group_id},
                new AppUser{ Age=22,RegsterTime=DateTime.Now,Introduce="蜀五虎将，定军扬威",ShowName="黄忠",UserName="huangzhong",Password="123456",Phone="13700000006",Gender=1,GroupId=shu_group_id},

                new AppUser{ Age=43,RegsterTime=DateTime.Now,Introduce="蜀君主，宁我负天人下，不可天下人负我",ShowName="曹操",UserName="caocao",Password="123456",Phone="13800000001",Gender=1,GroupId=wey_group_id,
                    RoleMappings=new System.Collections.Generic.List<AppUserRoleMapping>{ new AppUserRoleMapping {RoleId=sys_role_id }} },

                new AppUser{ Age=31,RegsterTime=DateTime.Now,Introduce="曹氏宗亲，拔矢啖睛",ShowName="夏侯惇",UserName="xiahoudun",Password="123456",Phone="13800000002",Gender=1,GroupId=wey_group_id},
                new AppUser{ Age=33,RegsterTime=DateTime.Now,Introduce="曹氏宗亲，虎步关右",ShowName="夏侯渊",UserName="xiahouyuan",Password="123456",Phone="13800000003",Gender=1,GroupId=wey_group_id},
                new AppUser{ Age=33,RegsterTime=DateTime.Now,Introduce="曹氏宗亲，拒超平叛",ShowName="曹仁",UserName="caoren",Password="123456",Phone="13800000004",Gender=1,GroupId=wey_group_id},
                new AppUser{ Age=33,RegsterTime=DateTime.Now,Introduce="五子良将，土垒督战",ShowName="于禁",UserName="yujin",Password="123456",Phone="13800000005",Gender=1,GroupId=wey_group_id},
                new AppUser{ Age=33,RegsterTime=DateTime.Now,Introduce="五子良将，其疾如风",ShowName="张辽",UserName="zhangliao",Password="123456",Phone="13800000006",Gender=1,GroupId=wey_group_id},
                new AppUser{ Age=33,RegsterTime=DateTime.Now,Introduce="五子良将，以逸待劳",ShowName="张郃",UserName="zhanghe",Password="123456",Phone="13800000007",Gender=1,GroupId=wey_group_id},
                new AppUser{ Age=33,RegsterTime=DateTime.Now,Introduce="五子良将，先登猛士",ShowName="乐进",UserName="yuejin",Password="123456",Phone="13800000008",Gender=1,GroupId=wey_group_id},
                new AppUser{ Age=33,RegsterTime=DateTime.Now,Introduce="五子良将，统军畏慎",ShowName="徐晃",UserName="xuhuang",Password="123456",Phone="13800000009",Gender=1,GroupId=wey_group_id}
            };

            context.Users.AddRange(users);
            context.SaveChanges();
        }
    }
}
